package com.tengju.motivation.domain.model.performance;

import com.tengju.motivation.domain.shared.BeanConvertUtil;
import com.tengju.motivation.domain.shared.DateUtil;
import com.tengju.motivation.domain.shared.Year;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * @author: 李大仙
 * @date: 2021/5/15 4:39 下午
 */
public class PerformanceObjectiveFactory {
    public static PerformanceObjective create(Long objectId, Integer type, String key, String name, String desc, Integer value, Integer startTime, Integer endTime, String staffName) {
        return new PerformanceObjective(null,
                new PerformanceObjectiveObjectId(objectId),
                PerformanceObjectiveTypeEnum.form(type),
                key, name, desc, BigDecimal.valueOf(value),
                startTime, endTime,
                PerformanceObjectiveStatusEnum.VALID,
                staffName, staffName,
                LocalDateTime.now(), LocalDateTime.now());
    }

    /**
     * 创建"春苗计划"用户目标
     *
     * @param chunMiaoTarget "春苗计划"目标
     * @return {@link List}<{@link PerformanceObjective}>
     */
    public static List<PerformanceObjective> createChunMiaoUserTarget(ChunMiaoTarget chunMiaoTarget, String updateBy) {
        Year year = new Year(String.valueOf(chunMiaoTarget.getDateForYear()));
        Integer startTime = DateUtil.getDayIntByLocalDate(year.getYearStartDate());
        Integer endTime = DateUtil.getDayIntByLocalDate(year.getYearEndDate());
        Map<String, Object> chunMiaoTargetMap = BeanConvertUtil.beanToMap(chunMiaoTarget);
        List<PerformanceObjective> chunMiaoTargetList = new ArrayList<>();
        for (ChunMiaoTargetEnum chunMiaoTargetEnum : ChunMiaoTargetEnum.values()){
            chunMiaoTargetList.add(new PerformanceObjective(null,
                    new PerformanceObjectiveObjectId(chunMiaoTarget.getIdCode().getValue()),
                    PerformanceObjectiveTypeEnum.CHUN_MIAO_PLAN_USER,
                    chunMiaoTargetEnum.getKey(), chunMiaoTargetEnum.getDesc(), ChunMiaoTarget.CHUN_MIAO_PLAN_PREFIX + chunMiaoTargetEnum.getDesc(),
                    // 目标值不设置则默认为 0
                    BigDecimal.valueOf(Optional.ofNullable((Long)chunMiaoTargetMap.get(chunMiaoTargetEnum.getKey())).orElse(0L)),
                    startTime, endTime,
                    PerformanceObjectiveStatusEnum.VALID,
                    updateBy, updateBy,
                    LocalDateTime.now(), LocalDateTime.now()));
        }
        return chunMiaoTargetList;
    }

}
